package common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.dao.JdbcDAO;
import common.pojo.BedType;

@Scope("prototype")
@Repository
public class BedTypeDAO extends JdbcDAO {

	/**
	 * 获取所有组信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<BedType> find(){
		String sql = "select * from bedType";
		return getList(sql,null,new BedTypeMapper());
	}
	/**
	 * 分页查询
	 * @param start
	 * @param limit
	 * @param orderby
	 * @param desc
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<BedType> findList(int start, int limit,String orderbys, String desc){
		String sql ="SELECT * FROM "+ 
		"(SELECT *, ROW_NUMBER() OVER (ORDER BY  sort"+" "+desc+") as rowNum "+
		"FROM bedType "+  
		") AS T "+ 
		"WHERE T.RowNum>"+start+ "and T.RowNum<="+(start+limit)+" order by sort"+" "+desc;
		return getList(sql,null,new BedTypeMapper());
	}
	
	/**
	 * 取得数据总数
	 * @return
	 */
	
	public int getCount(){
		return getCount("select count(*) from bedType",null);
	}
	
	
	public BedType findInfoById(int id){
		String sql ="select * from bedType where id ="+id;
		return (BedType)getObject(sql,null,new BedTypeMapper());
	}
	
	/**
	 * 更新数据库
	 * @param 
	 * @return
	 */
	public boolean update(BedType bedType){
		String sql ="update bedType set name='"+bedType.getName()+"' ,sort= "+bedType.getSort()+" where id = "+bedType.getId();
		update(sql);
		return true;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public boolean delete(int id){
		String sql ="delete from bedType where id = "+id;
		delete(sql);
		return true;
	}
	
	public void save(BedType bedType){
		String sql="insert into bedType values(?,?)";
		save(sql, new Object[] {bedType.getName(),bedType.getSort()});
	}
	
	/**
	 * 映射私有类
	 * @author pfp
	 *
	 */
	private static final class BedTypeMapper implements RowMapper {

		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			BedType bedType = new BedType();
			bedType.setId(rs.getInt(1));
			bedType.setName(rs.getString(2));
			bedType.setSort(rs.getInt(3));
			return bedType;
		}
	}
}
